package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.dingwei.superiordiary.R;
import com.lihao.baselibrary.utils.L;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 发布页面图片选择器中选择图片回调后展示的图片列表适配器
 *
 * @author Administrator
 */
public class PicAdapter extends BaseAdapter {
    private Context context;
    private List<LocalMedia> list;
    private Back back;
    private int viewwidth = 0;
    RequestOptions  options = new RequestOptions()
            .centerCrop()
            .placeholder(R.color.bg)
            .error(R.color.bg)
            .priority(Priority.HIGH)
            .diskCacheStrategy(DiskCacheStrategy.NONE);
    public interface Back {
         void back();

         void delete(int index);
    }

    public PicAdapter(Context context, List<LocalMedia> list, int width,
                      Back back) {
        super();
        this.context = context;
        this.list = list;
        this.back = back;
        viewwidth = (width - context
                .getResources().getDimensionPixelSize(R.dimen.dp_52)) / 3;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    public void setList(List<LocalMedia> list) {
        this.list = list;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View view, ViewGroup arg2) {
        ViewHolder viewHolder = null;
        if (null == view) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_pic, null);
            viewHolder = new ViewHolder(view);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHolder.allLayout.getLayoutParams();
            layoutParams.width = viewwidth;
            layoutParams.height = viewwidth;
            viewHolder.allLayout.setLayoutParams(layoutParams);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        LocalMedia photoItem = list.get(arg0);
//        String imageId = photoItem.getCompressPath();
//        L.e("------>"+imageId);
//        if (TextUtils.isEmpty(imageId)) {
//            L.e("------>"+1);
//            viewHolder.delete.setVisibility(View.GONE);
//            viewHolder.img.setImageResource(R.drawable.ic_upload);
//        } else {
//            L.e("------>"+2);
//            viewHolder.delete.setVisibility(View.VISIBLE);
//            String imagePath = photoItem.getCompressPath();
//            if (TextUtils.isEmpty(imagePath)) {
//                imagePath = "";
//            }
//            Glide.with(context)
//                    .load(imagePath)
//                    .apply(options)
//                    .into(viewHolder.img);
//        }

        if (TextUtils.isEmpty(photoItem.getCompressPath())) {
            viewHolder.img.setImageResource(R.drawable.ic_upload);
            viewHolder.delete.setVisibility(View.GONE);
        } else {
            viewHolder.img.setImageBitmap(BitmapFactory.decodeFile(photoItem.getCompressPath()));
            viewHolder.delete.setVisibility(View.VISIBLE);
        }

        viewHolder.img.setOnClickListener(new MyListener(arg0, "add", photoItem.getCompressPath()));
        viewHolder.delete.setOnClickListener(new MyListener(arg0, "delete",
                photoItem.getCompressPath()));

        return view;
    }

    class MyListener implements OnClickListener {
        private int index = 0;
        private String tag = "";
        private String imageId = "";

        public MyListener(int index, String tag, String imageId) {
            super();
            this.index = index;
            this.tag = tag;
            this.imageId = imageId;
        }

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if (tag.equals("delete")) {
                back.delete(index);
            } else {
                if (TextUtils.isEmpty(imageId)) {
                    back.back();
                }
            }
        }

    }

    class ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.delete)
        ImageView delete;
        @Bind(R.id.all_layout)
        FrameLayout allLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
